import pandas as pd
import multiprocessing
from functools import partial

import seaborn as sns
import matplotlib.pyplot as plt
import warnings

warnings.filterwarnings('ignore')
import pandas as pd
from score_transformer import *
from FeatureEngineer import *
from model_classify import *
from apply_rule import *
from sklearn.metrics import roc_curve
from metrics_select import *
from sklearn.metrics import roc_auc_score
from GetScore import *
from make_data import *
import random
import time
from until import *
import concurrent.futures

# if __name__ == '__main__':
#     param1 = 10
#     param2 = 20
#     pool = multiprocessing.Pool(processes=5)
#     partial_func = partial(process_dataframe, param1=param1, param2=param2)
#     results = pool.map(partial_func, range(0, 25, 5))
#     concat_df = pd.concat(results, ignore_index=True)
#     print(concat_df)



if __name__ == '__main__':
    s_time = time.time()
    sheet_names = ['dfwt', 'dfsc', 'df_dim', 'esg_rating', 'esg_index_data2','rule_config']

    with concurrent.futures.ProcessPoolExecutor() as executor:
        results = executor.map(read_sheet, sheet_names)

    dfwt,dfsc,df_dim,dfjch,dfo,rule_data = results

    # feature_map  = pd.read_excel('./data/feature_engine.xlsx')
    # dfwt = pd.read_excel('./data/dfwt.xlsx')
    # dfw = dfwt
    # dfsc = pd.read_excel('./data/dfsc.xlsx')
    # dfs = dfsc


    applyNull(dfo, lst90, 0.95)
    applyNull(dfo, lst50, 0.5)
    applyNull(dfo, lst30, 0.3)

    # dfjch['model_type'] = 'wllis'          #假装完成分群
    # %%

    # 模型分群
    codemap, classmap = get_dic()
    dfjch = to_classify(dfjch, codemap, classmap)
    #%%


    batch_id = 'ESG_DATA_202305'
    #%%
    model_type = ['A2','M1','M0','E0','H2','J1','J0','B2','C1','C0'] # ['M0','M1','E0','A2']
    pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
    partial_func = partial(process_dataframe,dfwt=dfwt,dfsc=dfsc,dfjch=dfjch,dfo=dfo,df_dim=df_dim,batch_id=batch_id,rule_data=rule_data)
    results = pool.map(partial_func,model_type)
    # print(results)
    result1 = [i[0] for i in results]
    result2 = [i[1] for i in results]
    result3 = [i[2] for i in results]
    esg_result_score = pd.concat(result1, ignore_index=True)
    esg_index_score = pd.concat(result2, ignore_index=True)
    esg_rule_record = pd.concat(result3, ignore_index=True)

    if 'esg_time' in esg_result_score.columns:
        esg_result_score['esg_time'] = time.strftime('%Y%m%d %H:%M:%S', time.localtime())
    # print(esg_result_score,esg_index_score,esg_rule_record)
    e_time = time.time()

    run_time = e_time - s_time
    print(run_time / 60, '分钟')


